import axios from 'axios'
import store from '../store'

export function request(config) {
    // 1. 创建axios实例
    const instance = axios.create({
        baseURL: 'http://chins-blog.me/api',
        timeout: 5000,
        headers: {
            'content-type': 'application/json'
        }
    })

    // 2. axios拦截器
    instance.interceptors.request.use(config => {
        if (store.state.token) {

            config.headers["Authorization"] = "Bearer " + localStorage.getItem("token")
        }
        return config
    }, err => {
        console.log('err ===> ' + err);
    })

    instance.interceptors.response.use(res => {
        console.log(res);
        if (res.data.status == 401) {
            alert("Token Invalid!");
        }
        return res.data;
    }, err => {
        console.log(err);
    })

    // 3. 发送真正的网络请求
    return instance(config);
    // 返回的是Promise，调用这个函数的时候直接then()就可以
}